<?js
var defPages = new Array(
	'index.' + JSWS_SCRIPT_FILEEXT,
	'default.' + JSWS_SCRIPT_FILEEXT,
	'index.html',
	'index.htm',
	'default.html',
	'default.htm'
	);

var scriptPath = script.path.clone();
var fullScriptPath = script.mapPath(scriptPath.path);

if (fullScriptPath.exists()) {

	if (fullScriptPath.isDirectory()) {
		if (!request.path.match(/\/$/)) {
			response.redirect(request.url + '/');
			response.end();
		}
		for (var i = 0; i < defPages.length; i++) {
			var tmpPath = fullScriptPath.clone();
			var defPage = defPages[i];
			tmpPath.append(defPage);
			if (tmpPath.exists() && tmpPath.isFile()) {
				server.tools.appendPath(fullScriptPath, defPage);
				scriptPath.append(defPage);
				break;
			}
		}
	}

	if (fullScriptPath.isDirectory())
		require("/../renderDir." + JSWS_SCRIPT_FILEEXT);
	else {
		var scriptRe = new RegExp("\\." + JSWS_SCRIPT_FILEEXT + "$", "i");

		if (scriptRe.exec(scriptPath.path)) {
			require(scriptPath.path);
		} else {
			// TODO: move mime-service to server object
			var mimeService = Components.classes['@mozilla.org/mime;1']
				.getService(Components.interfaces.nsIMIMEService);
			try { response.contentType(mimeService.getTypeFromFile(fullScriptPath)); } catch (ex) { }

			response.write(readFile(scriptPath.path));
		}
	}
} else {
	response.clear();
	require("/../404." + JSWS_SCRIPT_FILEEXT);
	response.status = 404;
}
?>